#!/usr/bin/env bash

function choice()
{
    find "$HOME/.password-store" -name \*.gpg | sed 's/^.*\.password-store\///g' | sed 's/\.gpg$//g' | bemenu -il6 -p "choose $1"
}

case $1 in
    generate)
        NAME="$(choice "name for new password")"
        if [ -z $NAME ]; then exit; fi
        
        notify-send "password generated" "$(pass generate -c $NAME)"
        ;;
    rm)
        NAME="$(choice "password to delete")"
        if [ -z $NAME ]; then exit; fi
        
        notify-send "password deleted" "$(pass rm -f $NAME)"
        ;;
    otp)
        OTP="$(choice otp)"
        if [ -z $OTP ]; then exit; fi

        notify-send "otp code copied" "$(pass otp -c $OTP)"
        ;;
    *)
        PASSWORD="$(choice password)"
        if [ -z $PASSWORD ]; then exit; fi

        notify-send "password copied" "$(pass -c $PASSWORD)"
        ;;
esac
